
##############################
### Plot for election maps ###
##############################

rm(list = ls())
gc()

library(data.table)
library(tidyverse)
library(janitor)

### Set WD
setwd("~/Dropbox (Princeton)/Research/*Spatial_Inequality/paper_SMD_inequality/JoP_final/replication")  

### Define function
my_pal <- function(range = c(1, 6)) {
  force(range)
  function(x) scales::rescale(x, to = range, from = c(0, 1))
}

### Load data
load("data/elections.RData")

setkey(elections, year, type, winner)


### Table 3: Constituencies won

# Tories
tabyl(elections, type, year, winner)$Conservative

# Labour
tabyl(elections[ ], type, year, winner)$Labour

### Table 3: Average population density 

# Tories
elections[ winner %in% c("Conservative") , round(mean(pop_density),0) , by=c("type","year","winner") ]

# Labour
elections[ winner %in% c("Labour") , round(mean(pop_density),0) , by=c("type","year","winner") ]


### Data for plot
d_full <- rbind(elections[ year == 2010 ,.(winner,diff_mean_med_tot_inc_log,diff_lab_minus_con,year,pop_density)],
                elections[ year == 2015 ,.(winner,diff_mean_med_tot_inc_log,diff_lab_minus_con,year,pop_density)],
                elections[ year == 2017 ,.(winner,diff_mean_med_tot_inc_log,diff_lab_minus_con,year,pop_density)],
                elections[ year == 2019 ,.(winner,diff_mean_med_tot_inc_log,diff_lab_minus_con,year,pop_density)])

d_full[ , year2 := paste(year,"Election", sep=" ")]

pdf("out/fig7.pdf", width=10, height=8) # pcon_vote_ineq_density_comb_fin_v4.pdf
(plot_all <- (ggplot(data=d_full[ !is.na(winner) ], aes(y=diff_mean_med_tot_inc_log, x=diff_lab_minus_con))
              + geom_vline(xintercept = 0, colour="gray30", linetype="dashed", linewidth=0.8)
              + geom_hline(yintercept = median(d_full$diff_mean_med_tot_inc_log), colour="gray30", linetype="dashed", linewidth=0.8)
              + geom_point(aes(fill=winner, size=pop_density), colour="gray20", shape=21, alpha=0.4)
              + continuous_scale(aesthetics = c("size", "point.size"), name = "Population density",
                                 scale_name = "size", palette = my_pal(c(1.5,11)), guide = guide_legend(override.aes = list(label = ""))) 
              + annotate(geom="text", y=7.15, x=82, label="stronger Labour", size=2.75)
              + annotate("segment", y = 7, yend = 7, x = 71, xend = 95, size=0.4, colour="gray25", arrow=arrow(length=unit(0.30,"cm"), type = "closed", angle = 20))
              + annotate(geom="text", y=12, x=-52, label="more unequal", size=2.75)
              + annotate("segment", y = 11.3, yend = 11.9, x = -62, xend = -62, size=0.4, colour="gray25", arrow=arrow(length=unit(0.30,"cm"), type = "closed", angle = 20))
              + annotate(geom="text", y=median(d_full$diff_mean_med_tot_inc_log), x=87, label="median\ninequality", size=3)
              + ylab("Mean-median income inequality, log £")
              + xlab("Difference between Labour and Tory vote share (%)")
              + facet_wrap(~ year2)
              + theme_bw()
              + theme(legend.position="right", panel.border = element_rect(color="gray"), panel.grid.minor = element_blank(), plot.title = element_text(hjust = 0.5))
              + scale_fill_manual("Election winner", values=c("blue","red","yellow","green","white"))
              + guides(fill = guide_legend(override.aes = list(size=5), order=1))
              + coord_cartesian(ylim=c(7.1,11.9), xlim=c(-60,92))
              + scale_x_continuous(breaks=seq(-50,90,25))
))
dev.off()


